package com.ruoyi.generator.domain

import com.ruoyi.common.core.domain.BaseEntity
import com.ruoyi.common.utils.MyStringUtils
import org.apache.commons.lang3.StringUtils
import javax.validation.constraints.NotBlank

/**
 * 代码生成业务字段表 gen_table_column
 *
 * @author ruoyi
 */
class GenTableColumn : BaseEntity() {
    /** 编号  */
    @JvmField
    var columnId: Long? = null

    /** 归属表编号  */
    @JvmField
    var tableId: Long? = null

    /** 列名称  */
    @JvmField
    var columnName: String? = null

    /** 列描述  */
    var columnComment: String? = null

    /** 列类型  */
    @JvmField
    var columnType: String? = null

    /** JAVA类型  */
    @JvmField
    var javaType: String? = null

    /** JAVA字段名  */
    @JvmField
    var javaField: @NotBlank(message = "Java属性不能为空") String? = null

    /** 是否主键（1是）  */
    var isPk: String? = null

    /** 是否自增（1是）  */
    var isIncrement: String? = null

    /** 是否必填（1是）  */
    @JvmField
    var isRequired: String? = null

    /** 是否为插入字段（1是）  */
    @JvmField
    var isInsert: String? = null

    /** 是否编辑字段（1是）  */
    @JvmField
    var isEdit: String? = null

    /** 是否列表字段（1是）  */
    @JvmField
    var isList: String? = null

    /** 是否查询字段（1是）  */
    @JvmField
    var isQuery: String? = null

    /** 查询方式（EQ等于、NE不等于、GT大于、LT小于、LIKE模糊、BETWEEN范围）  */
    @JvmField
    var queryType: String? = null

    /** 显示类型（input文本框、textarea文本域、select下拉框、checkbox复选框、radio单选框、datetime日期控件、image图片上传控件、upload文件上传控件、editor富文本控件）  */
    @JvmField
    var htmlType: String? = null

    /** 字典类型  */
    @JvmField
    var dictType: String? = null

    /** 排序  */
    var sort: Int? = null
    val capJavaField: String
        get() = StringUtils.capitalize(javaField)

    fun isPk(): Boolean {
        return isPk(isPk)
    }

    fun isPk(isPk: String?): Boolean {
        return isPk != null && StringUtils.equals("1", isPk)
    }

    fun isIncrement(): Boolean {
        return isIncrement(isIncrement)
    }

    fun isIncrement(isIncrement: String?): Boolean {
        return isIncrement != null && StringUtils.equals("1", isIncrement)
    }

    fun isRequired(): Boolean {
        return isRequired(isRequired)
    }

    fun isRequired(isRequired: String?): Boolean {
        return isRequired != null && StringUtils.equals("1", isRequired)
    }

    fun isInsert(): Boolean {
        return isInsert(isInsert)
    }

    fun isInsert(isInsert: String?): Boolean {
        return isInsert != null && StringUtils.equals("1", isInsert)
    }

    fun isEdit(): Boolean {
        return isInsert(isEdit)
    }

    fun isEdit(isEdit: String?): Boolean {
        return isEdit != null && StringUtils.equals("1", isEdit)
    }

    fun isList(): Boolean {
        return isList(isList)
    }

    fun isList(isList: String?): Boolean {
        return isList != null && StringUtils.equals("1", isList)
    }

    fun isQuery(): Boolean {
        return isQuery(isQuery)
    }

    fun isQuery(isQuery: String?): Boolean {
        return isQuery != null && StringUtils.equals("1", isQuery)
    }

    val isSuperColumn: Boolean
        get() = isSuperColumn(javaField)
    val isUsableColumn: Boolean
        get() = isUsableColumn(javaField)

    fun readConverterExp(): String? {
        val remarks: String = StringUtils.substringBetween(columnComment, "（", "）")
        val sb = StringBuffer()
        return if (MyStringUtils.isNotEmpty(remarks)) {
            for (value in remarks.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
                if (MyStringUtils.isNotEmpty(value)) {
                    val startStr: Any = value.subSequence(0, 1)
                    val endStr = value.substring(1)
                    sb.append("").append(startStr).append("=").append(endStr).append(",")
                }
            }
            sb.deleteCharAt(sb.length - 1).toString()
        } else {
            columnComment
        }
    }

    companion object {
        private const val serialVersionUID = 1L
        fun isSuperColumn(javaField: String?): Boolean {
            return StringUtils.equalsAnyIgnoreCase(javaField,  // BaseEntity
                    "createBy", "createTime", "updateBy", "updateTime", "remark",  // TreeEntity
                    "parentName", "parentId", "orderNum", "ancestors")
        }

        fun isUsableColumn(javaField: String?): Boolean {
            // isSuperColumn()中的名单用于避免生成多余Domain属性，若某些属性在生成页面时需要用到不能忽略，则放在此处白名单
            return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum", "remark")
        }
    }
}
